package com.lsy.weizhi2.utils;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.CountDownTimer;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.lsy.weizhi2.JsonObject.LoginObject;
import com.lsy.weizhi2.JsonObject.Register_Login_Send;
import com.lsy.weizhi2.JsonObject.Verify_Send;
import com.lsy.weizhi2.activity.HomePage;
import com.lsy.weizhi2.activity.LoginActivty;
import com.lsy.weizhi2.config.Constants;
import com.lsy.weizhi2.config.MyInfo;

import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import com.lsy.weizhi2.R;
/**
 * Created by 嗨哟 on 2017/2/7.
 */
public class MyMethod {
    //直接跳转新的Activity
    public static void toActivity(Context context,Class mClass){
        Intent intent=new Intent(context,mClass);
        context.startActivity(intent);
    }
    //带参数跳转新的Activity
    public static void toActivity(Context context,Class mClass,String[]keys,String[]values){
        Intent intent=new Intent(context,mClass);
        if(keys!=null&&values!=null&&keys.length==values.length){
            for (int i = 0; i < keys.length; i++) {
                intent.putExtra(keys[i],values[i]);
            }
        }
        context.startActivity(intent);
    }
    //带参数跳转新的Activity
    public static void toActivity(Context context,Class mClass,String[]keys,int[]values){
        Intent intent=new Intent(context,mClass);
        if(keys!=null&&values!=null&&keys.length==values.length){
            for (int i = 0; i < keys.length; i++) {
                intent.putExtra(keys[i],values[i]);
            }
        }
        context.startActivity(intent);
    }
    //测试showToast
    public static void showToast(Context context,String msg){
        Toast.makeText(context,msg,Toast.LENGTH_SHORT).show();
    }
    //测试showToast
    public static void showToast(Context context,int msg){
        Toast.makeText(context,msg,Toast.LENGTH_SHORT).show();
    }
    //时间戳转
    public static void getDate(int timestamp){

    }
    public static String[] getDateArray(int timeStamp) {
        Log.i("HomeAdapter","timeStamp==="+timeStamp);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒", Locale.CHINA);
        String datetimes = dateFormat.format(new Date(timeStamp * 1000L));
        String[] fenge = datetimes.split("[年月日时分秒]");
        return fenge;
    }
    /**
     * 根据传递的类型格式化时间
     *
     * @param str
     * @param type 例如：yy-MM-dd
     * @return
     */
    public static String getDateTimeByMillisecond(String str, String type) {

        Date date = new Date(Long.valueOf(str)*1000);

        SimpleDateFormat format = new SimpleDateFormat(type);

        String time = format.format(date);

        return time;
    }
    /**
     * 判断网络是否连接.
     */
    public static boolean isNetworkConnected(Context context) {
        ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
        return mNetworkInfo != null && mNetworkInfo.isAvailable();
    }
    /**
     * 判断是否存在某个包.
     */
    public static boolean isPkgInstalled(Context context,String pkgName) {
        PackageInfo packageInfo = null;
        try {
            packageInfo = context.getPackageManager().getPackageInfo(pkgName, 0);
        } catch (PackageManager.NameNotFoundException e) {
            packageInfo = null;
            e.printStackTrace();
        }
        if (packageInfo == null) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * 批量获得view内的value
     * 主要用于批量TextView获取数据
     * @param tvs view数组
     * @return  得到的值的数组
     * @throws NoSuchMethodException 找不到该方法的异常
     * @throws InvocationTargetException 调用目标异常
     * @throws IllegalAccessException   类型转换异常
     */

    public static String[] getTextS(View...tvs) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        String[] strs=new String[tvs.length];
        Method method=tvs[0].getClass().getDeclaredMethod("getText",new Class[]{});
        Log.i("MyMethod","method=="+method);
        for (int i = 0; i < tvs.length; i++) {
            /**
             * 利用反射循环加载数据
             */

                strs[i]= (String) method.invoke(tvs[i],new Object[]{});
        }
        return strs;
    }

    public static void CallPhone(Context context,String phone){
        Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phone));
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        if (ActivityCompat.checkSelfPermission(context, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
        }
        context.startActivity(intent);
    }

    public static void ShowDialog(Context context,String msg){
        new AlertDialog.Builder(context).setMessage(msg).setNegativeButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.dismiss();
            }
        }).setPositiveButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.dismiss();
            }
        }).show();
    }

    public static void ShowDialogLogin(final Context context, final Class mClass){
        new AlertDialog.Builder(context).setMessage("账号在其他地方登录，请重新登录修改密码").setNegativeButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                toActivity(context,mClass);
                dialogInterface.dismiss();
                ((Activity)context).finish();
            }
        }).show();
    }

    public static void ShowDialogNet(final Context context){
        new AlertDialog.Builder(context).setMessage("网络连接故障").setNegativeButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.dismiss();
            }
        }).show();
    }

    public static void ShowDiaLogFinish(final Context context, String msg){
        new AlertDialog.Builder(context).setMessage(msg).setNegativeButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.dismiss();
                ((Activity)context).finish();
            }
        }).show();
    }
}
